package com.pyl.admin.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pyl.admin.core.enums.ResultEnum;
import com.pyl.admin.core.enums.StatusEnum;
import com.pyl.admin.core.exception.ResultException;
import com.pyl.admin.core.shiro.ShiroUtil;
import com.pyl.admin.core.web.PageUtil;
import com.pyl.admin.core.web.PageUtil.PageSort;
import com.pyl.admin.system.entity.SysRole;
import com.pyl.admin.system.entity.SysRoleMenu;
import com.pyl.admin.system.mapper.SysRoleMapper;
import com.pyl.admin.system.mapper.SysRoleMenuMapper;
import com.pyl.admin.system.service.SysRoleService;
import com.pyl.admin.system.validator.RoleForm;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 灵魂贰婶
 * @since 2019-06-28
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	public List<SysRole> selectUserRoleList(Long sysUserId, Integer status) {

		return sysRoleMapper.selectUserRoleList(sysUserId, status);
	}

	@Override
	public PageInfo<SysRole> selectRoelList(Integer status, String query) {
		PageSort pageSort = PageUtil.pageRequest();
		PageHelper.startPage(pageSort.getCurrent(), pageSort.getSize());
		PageHelper.orderBy(pageSort.getOrderByColumn()+ " " + pageSort.getIsAsc());
		List<SysRole> list = sysRoleMapper.selectRoelList(status, query);
		PageInfo<SysRole> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	@Transactional
	public boolean saveOrUpdate(RoleForm roleForm) {

		if (ObjectUtil.isNotNull(roleForm.getId())) {
			SysRole sysRole = sysRoleMapper.selectById(roleForm.getId());
			BeanUtil.copyProperties(roleForm, sysRole, CopyOptions.create().setIgnoreNullValue(true));
			sysRole.setUpdateDate(new Date());
			sysRole.setUpdateBy(ShiroUtil.getSubject().getId());
			sysRole.setUpdateByName(ShiroUtil.getSubject().getNickname());
			return SqlHelper.retBool(sysRoleMapper.updateById(sysRole));
		}
		// 判断角色是否存在
		if (ObjectUtil.isNotNull(sysRoleMapper.selectOne(new SysRole().setName(roleForm.getName())))) {
			throw new ResultException(ResultEnum.ROLE_EXIST);
		}
		SysRole sysRole = new SysRole();
		BeanUtil.copyProperties(roleForm, sysRole, CopyOptions.create().setIgnoreNullValue(true));
		sysRole.setStatus(Integer.valueOf(StatusEnum.OK.getCode()));
		sysRole.setCreateDate(new Date());
		sysRole.setCreateBy(ShiroUtil.getSubject().getId());
		sysRole.setCreateByName(ShiroUtil.getSubject().getNickname());
		sysRole.setUpdateDate(new Date());
		sysRole.setUpdateBy(ShiroUtil.getSubject().getId());
		sysRole.setUpdateByName(ShiroUtil.getSubject().getNickname());
		return SqlHelper.retBool(sysRoleMapper.insert(sysRole));
	}

	@Override
	public SysRole selectRoleMenuById(Long id) {

		return sysRoleMapper.selectRoleMenuById(id);
	}

	@Override
	@Transactional
	public boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
		// 删除 则删除所有菜单
		if (statusEnum == StatusEnum.DELETE) {
			for (int i = 0; i < idList.size(); i++) {
				// 删除角色菜单
				sysRoleMenuMapper.delete(new EntityWrapper<SysRoleMenu>().eq("role_id", idList.get(i)));
			}
		}
		SysRole sysRole = new SysRole();
		sysRole.setStatus(Integer.valueOf(statusEnum.getCode()));
		sysRole.setUpdateDate(new Date());
		sysRole.setUpdateBy(ShiroUtil.getSubject().getId());
		sysRole.setUpdateByName(ShiroUtil.getSubject().getNickname());
		return SqlHelper.retBool(sysRoleMapper.update(sysRole, new EntityWrapper<SysRole>().in("id", idList)));
	}

}
